﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IDAL;

namespace Skyworth.CRM.DAL
{
   public class ProductTypeDAL:SuperDataBase,IProductTypeDAL
    {
       public DataTable GetAllProductTypeByBrand(int brandId)
       {
           string strSql=" select * from T_BasicData_ProductType where BrandId=@brandId ";
           DbCommand cmd = DB.GetSqlStringCommand(strSql);
           DB.AddInParameter(cmd, "@brandId", DbType.Int16, brandId);
           return DB.ExecuteDataSet(cmd).Tables[0];
       }

       public DataTable GetAllSubProductType(int parentId)
       {
           throw new NotImplementedException();
       }

       public void Add(ProductType model)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append(" insert into T_BasicData_ProductType(");
           strSql.Append(" Code,Name,ParentId,level,BrandId,Description)");
           strSql.Append(" values(@code,@Name,@ParentId,@level,@brandId,@description) ");

           DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
           DB.AddInParameter(cmd, "@Name", DbType.String, model.Name);
           DB.AddInParameter(cmd, "@code", DbType.String, model.Code);
           DB.AddInParameter(cmd, "@ParentId", DbType.Int16, model.ParentId);
           DB.AddInParameter(cmd, "@level", DbType.Int16, model.Level);
           DB.AddInParameter(cmd, "@brandId", DbType.Int16, model.BrandId);
           DB.AddInParameter(cmd, "@description", DbType.String, model.Description);
           DB.ExecuteNonQuery(cmd);
       }

       public bool Update(ProductType model)
       {
          
           StringBuilder strSql = new StringBuilder();
           strSql.Append(" update T_BasicData_ProductType set ");
           strSql.Append(" Name=@name,");
           strSql.Append(" Code=@code,");
           strSql.Append(" ParentId=@parentId,");
           strSql.Append(" level=@level,");
           strSql.Append(" BrandId=@brandId,");
           strSql.Append(" Description=@description ");
           strSql.Append(" where Id=@id");

           DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
           DB.AddInParameter(cmd, "@name", DbType.String, model.Name);
           DB.AddInParameter(cmd, "@code", DbType.String, model.Code);
           DB.AddInParameter(cmd, "@parentId", DbType.Int16, model.ParentId);
           DB.AddInParameter(cmd, "@level", DbType.Int16, model.Level);
           DB.AddInParameter(cmd, "@brandId", DbType.Int16, model.BrandId);
           DB.AddInParameter(cmd, "@id", DbType.Int32, model.Id);
           DB.AddInParameter(cmd, "@description", DbType.String, model.Description);

           DB.ExecuteNonQuery(cmd);

          return true;
       }

       public bool Delete(int productTypeId)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append(" delete T_BasicData_ProductType where Id=@id or ParentId=@id ");
           DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
           DB.AddInParameter(cmd, "@id", DbType.Int32, productTypeId);
           DB.ExecuteNonQuery(cmd);

           return true;
       }
    }
}
